package com.example.light.web.personal.controller;

import com.example.light.common.exception.GlobalException;
import com.example.light.common.result.CodeMsg;
import com.example.light.common.result.Result;
import com.example.light.web.personal.entity.Schedual;
import com.example.light.web.personal.service.ScheduleService;
import com.example.light.web.personal.vo.ScheduleVO;
import com.example.light.web.system.entity.User;
import org.apache.commons.beanutils.BeanUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("schedule")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    //---------------- 跳转页面 ----------------
    @RequestMapping("toScheduleAdd")
    public String showScheduleAdd(){
        return "personal/schedual_add";
    }

    @RequestMapping("toScheduleModify/{scheduleId}")
    public String showScheduleModify(@PathVariable("scheduleId")Integer scheduleId,Model model){
        ScheduleVO schedule = scheduleService.showOneSchedule(scheduleId);
        model.addAttribute("schedule",schedule);
        return "personal/schedual_update";
    }

    @RequestMapping("showSchedules")
    public String showScheduleList(Model model,User user){
        List<ScheduleVO> schedules = scheduleService.showSchedules(user);
        model.addAttribute("schedules",schedules);
        return "personal/schedule_list";
    }

    @RequestMapping("addSchedule")
    @ResponseBody
    public Result<Boolean> addSchedule(User user, ScheduleVO scheduleVO, BindingResult result){
        if(scheduleVO.getSchedualTitle().equals("") || scheduleVO.getSchedualContent().equals("")
                || scheduleVO.getSchedualStart().equals("") || scheduleVO.getSchedualEnd().equals("")){
            throw new GlobalException(CodeMsg.PersonalCodeMsg.NULL_SCHEDULE);
        }
        String schedualStart = scheduleVO.getSchedualStart();
        String schedualEnd = scheduleVO.getSchedualEnd();
        DateTime start = DateTime.parse(schedualStart, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
        DateTime end = DateTime.parse(schedualEnd, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
        if (start.isAfter(end)){
            throw new GlobalException(CodeMsg.PersonalCodeMsg.TIME_ERROR);
        }
        boolean schedulesSucc =scheduleService.addSchedule(scheduleVO,user.getUserId());
        if (schedulesSucc) {
            return Result.success();
        }
        return Result.error(CodeMsg.PersonalCodeMsg.ADD_SCHEDULE_FAIL);
    }

    @RequestMapping("modifySchedule")
    @ResponseBody
    public Result<Boolean> modifySchedule(User user, ScheduleVO scheduleVO, BindingResult result){
        if(scheduleVO.getSchedualTitle().equals("") || scheduleVO.getSchedualContent().equals("")
                || scheduleVO.getSchedualStart().equals("") || scheduleVO.getSchedualEnd().equals("")){
            throw new GlobalException(CodeMsg.PersonalCodeMsg.NULL_SCHEDULE);
        }
        String schedualStart = scheduleVO.getSchedualStart();
        String schedualEnd = scheduleVO.getSchedualEnd();
        DateTime start = DateTime.parse(schedualStart, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
        DateTime end = DateTime.parse(schedualEnd, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
        if (start.isAfter(end)){
            throw new GlobalException(CodeMsg.PersonalCodeMsg.TIME_ERROR);
        }
        boolean schedulesSucc =scheduleService.modifySchedule(scheduleVO,user.getUserId());
        if (schedulesSucc) {
            return Result.success();
        }
        return Result.error(CodeMsg.PersonalCodeMsg.MODIFY_SCHEDULE_FAIL);
    }


    @RequestMapping("del/{scheduleId}")
    public String delSchedule(@PathVariable("scheduleId")Integer id){
        scheduleService.delSchedule(id);
        return "redirect:/schedule/showSchedules";
    }

    @RequestMapping("finish/{scheduleId}")
    public String finishSchedule(@PathVariable("scheduleId")Integer id){
        scheduleService.finish(id);
        return "redirect:/schedule/showSchedules";
    }
}
